package com.ytzl.dao.impl;

import com.ytzl.dao.Raindao;
import com.ytzl.pojo.Rains;
import com.ytzl.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Raindaoimpl extends BaseDao implements Raindao {
    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public List<Rains> getAll() {

        List<Rains>rains=new ArrayList<>();
        Rains rains1 =null;
        ResultSet rs;
        String sql ="select * from rains";
        rs=this.exceuteQuery(sql);
        try {
            while (rs.next()){
                rains1=new Rains();
                rains1.setId(rs.getInt("id"));
                rains1.setCityname(rs.getString("cityname"));
                rains1.setCreatetime(rs.getDate("createtime"));
                rains1.setRainvalue(rs.getInt("rainvalue"));
                rains1.setDetection(rs.getString("detection"));
                rains1.setAddress(rs.getString("address"));

                rains.add(rains1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
        }
        return rains;
    }

    @Override
    public int addRain(Rains rains) {
        int result =0;
        try {
            StringBuilder sql=new StringBuilder();
            sql.append("insert into rains(cityname,createtime,rainvalue,detection,address)");
            sql.append(" values(?,?,?,?,?)");
            this.exceuteUpdate(sql.toString(),rains.getCityname(),rains.getCreatetime(),rains.getRainvalue(),rains.getDetection(),rains.getAddress());
        } catch (Exception e) {
            e.printStackTrace();
            result =-1;
        }

        return result;
    }

    @Override
    public int delRain(int id) {
        try {
            StringBuilder sql=new StringBuilder();
            sql.append("delete from rains");
            sql.append(" where id = ?");
            return this.exceuteUpdate(sql.toString(),id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }
}
